<?php
/**
 * Created by PhpStorm.
 * User: mingzhongshui
 * Date: 2020/3/31
 * Time: 21:09
 */

namespace App\Repositories\User;

use App\Repositories\Repository;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

/**
 * Class UserRepository
 * @package App\Repositories\User
 */
class UserRepository extends Repository
{

    const IS_VIP = 1;
    const NOT_VIP = 0;


    /**
     * 用户状态
     */
    const STATUS_DEFAULT = 0; // 未注册（默认）
    const STATUS_BLACK = 1; // 已拉黑
    const STATUS_REGISTER = 2; // 已注册

    /**
     * 是否关注功能公众号
     */
    const IS_SUBSCRIBE = 1; // 关注

    /**
     * @return string
     */
    public function model()
    {
        return \App\Models\User\UserModel::class;
    }

    /**
     * @return mixed
     */
    public function getNeedRemindVipUser()
    {
        return $this->model
                    ->where('is_vip', self::IS_VIP)
                    ->where(DB::raw("(vip_expire_time - 604800)"), '<=', time())
                    ->where('vip_expire_time', '>', 0)
                    ->get();
    }

    /**
     * @return mixed
     */
    public function getExpireVipUser()
    {
        $time = time();
        
        return $this->model
                    ->where('vip_expire_time', '<=', $time)
                    ->where('is_vip', self::IS_VIP)
                    ->where('vip_expire_time', '>', 0)
                    ->pluck('id');
    }

    /**
     * @param $uid
     * @return mixed
     */
    public function resetUserVip($uid)
    {
        return $this->where('id', $uid)->update(['is_vip' => self::NOT_VIP, 'vip_expire_time' => 0]);
    }

    /**
     * @param $industryIds
     * @return mixed
     */
    public function getUidsByIndustryIds($industryIds)
    {
        return $this->model->whereIn('industry_id', $industryIds)->pluck('id', 'id');
    }

    /**
     * @param $address
     * @return mixed
     */
    public function getUidsByAddress($address)
    {
        return $this->model->where('address', 'like', "%{$address}%")->pluck('id', 'id');
    }

    /**
     * @param $uid
     * @return mixed
     */
    public function getOneById($uid)
    {
        return $this->model->where('id', $uid)->first();
    }

    /**
     * @param $push
     * @param int $lastId
     * @return mixed
     */
    public function getUserIdsByPush($push, $lastId = 0)
    {
        $query = $this->model
                    ->leftJoin('user_label', 'user.id', '=', 'user_label.user_id')
                    ->where('user.status', self::STATUS_REGISTER)
                    ->where('user.is_subscribe', self::IS_SUBSCRIBE);

        if ($push->province_id == 'all') {
            $query->where('user.address', '!=', "");
        } else if ($push->province_id) {

            list($province, $city) = explode(' ', $push->address);

            if ($push->city_id == 'all') {
                $query->where(function ($query) use ($province) {
                    $query->where('user.address', 'like', "%{$province}")->orWhere('user.c_address', 'like', "%{$province}");
                });
            } else {
                $query->where(function ($query) use ($city) {
                    $query->where('user.address', 'like', "%{$city}%")->orWhere('user.c_address', 'like', "%{$city}%");
                });
            }
        }

        if ($push->industry_ids && $push->label_ids) {

            $labelIds = explode(',', $push->label_ids);
            $industryIds_ori = explode(',', $push->industry_ids);

            $industryIds_count=floor(count($industryIds_ori)/3);
            $industryIds=array();
            for($i=0;$i<$industryIds_count;$i++){
                if(!empty($industryIds_ori[2+$i*3])){
                    $industryIds[]=$industryIds_ori[2+$i*3];
                }else if(!empty($industryIds_ori[1+$i*3])){
                    $industryIds[]=$industryIds_ori[1+$i*3];
                }else if(!empty($industryIds_ori[$i*3])){
                    $industryIds[]=$industryIds_ori[$i*3];
                }
            }

            $query->where(function ($query) use ($industryIds, $labelIds) {
                $query->whereIn('user.industry_id', $industryIds)->orWhereIn('user_label.label_id', $labelIds);
            });
        } else {

            if ($push->industry_ids) {
                $industryIds_ori = explode(',', $push->industry_ids);
                $industryIds_count=floor(count($industryIds_ori)/3);
                $industryIds=array();
                for($i=0;$i<$industryIds_count;$i++){
                    if(!empty($industryIds_ori[2+$i*3])){
                        $industryIds[]=$industryIds_ori[2+$i*3];
                    }else if(!empty($industryIds_ori[1+$i*3])){
                        $industryIds[]=$industryIds_ori[1+$i*3];
                    }else if(!empty($industryIds_ori[$i*3])){
                        $industryIds[]=$industryIds_ori[$i*3];
                    }
                }
                $query->whereIn('user.industry_id', $industryIds);
            }

            if ($push->label_ids) {
                $labelIds = explode(',', $push->label_ids);
                $query->whereIn('user_label.label_id', $labelIds);
            }
        }

        if ($lastId) {
            $query->where('user.id', '>', $lastId);
        }

        return $query->groupBy('user.id')->orderBy('user.id', 'asc')->limit(100)->pluck('user.id');
    }
}
